package com.gugee.tiktok.common.model.dal.challenge.impl;

import com.gugee.tiktok.common.model.dal.challenge.ChallengeExtRepository;
import com.gugee.tiktok.common.model.mysql.Challenge;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.util.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class ChallengeExtRepositoryImpl  implements ChallengeExtRepository {

    @Override
    public List<Challenge> findByCid(List<Challenge> challenges) {
        String sql = "SELECT * FROM challenge WHERE cid in ({cids})";
        String cids = "";
        for (Challenge challenge : challenges) {
            if(StringUtils.isEmpty(cids)){
                cids += challenge.getCid();
                continue;
            }
            cids += ","+challenge.getCid();
        }
        sql = sql.replace("{cids}",cids);
        return JdbcTemplateHelper.findEntity(Challenge.class,sql);
    }

    @Override
    public void batchUpdate(List<Challenge> challenges) {
        JdbcTemplateHelper.getJdbcTemplate().batchUpdate(
                "UPDATE `tiktok`.`challenge` SET  " +
                        "`cid` = ?, `cha_name` = ?, `desc` = ?, `is_commerce` = ?, `view_count` = ?, `view_growth` = ?," +
                        " `user_count` = ?, `user_growth` = ?, `region` = ?, `cover_photo` = ?, `share_url` = ?, " +
                        "`qr_code` = ?, `top_videos` = ?, `week_growth` = ?,`update_time` = ?," +
                        " `update_by` = ?" +
                        " WHERE `id` = ?"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Challenge challenge = challenges.get(i);
                        preparedStatement.setLong(1, challenge.getCid());
                        preparedStatement.setString(2, challenge.getChaName());
                        preparedStatement.setString(3, challenge.getDesc());
                        preparedStatement.setBoolean(4, challenge.getIsCommerce());
                        preparedStatement.setLong(5, challenge.getViewCount());
                        preparedStatement.setLong(6, challenge.getViewGrowth());
                        preparedStatement.setLong(7, challenge.getUserCount());
                        preparedStatement.setLong(8, challenge.getUserGrowth());
                        preparedStatement.setString(9, challenge.getRegion());
                        preparedStatement.setString(10, challenge.getCoverPhoto());
                        preparedStatement.setString(11, challenge.getShareUrl());
                        preparedStatement.setString(12, challenge.getQrCode());
                        preparedStatement.setString(13, challenge.getTopVideos());
                        preparedStatement.setString(14, challenge.getWeekGrowth());
                        preparedStatement.setLong(15, challenge.getUpdateTime());
                        preparedStatement.setString(16, challenge.getUpdateBy());
                        preparedStatement.setLong(17, challenge.getId());

                    }
                    @Override
                    public int getBatchSize() {
                        return challenges.size();
                    }
                });
    }

    @Override
    public void insertUpdate(List<Challenge> challenges) {

        JdbcTemplateHelper.getJdbcTemplate().batchUpdate(
                "INSERT INTO `tiktok`.`challenge` " +
                        "(`cid`, `cha_name`, `desc`, `is_commerce`, `view_count`, `view_growth`," +
                        " `user_count`, `user_growth`, `region`, `cover_photo`, `share_url`, " +
                        "`qr_code`, `top_videos`, `week_growth`,`update_time`," +
                        " `update_by`,`create_time`,`create_by`)" +
                        "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        Challenge challenge = challenges.get(i);
                        preparedStatement.setLong(1, challenge.getCid());
                        preparedStatement.setString(2, challenge.getChaName());
                        preparedStatement.setString(3, challenge.getDesc());
                        preparedStatement.setBoolean(4, challenge.getIsCommerce());
                        preparedStatement.setLong(5, challenge.getViewCount());
                        preparedStatement.setLong(6, challenge.getViewGrowth());
                        preparedStatement.setLong(7, challenge.getUserCount());
                        preparedStatement.setLong(8, challenge.getUserGrowth());
                        preparedStatement.setString(9, challenge.getRegion());
                        preparedStatement.setString(10, challenge.getCoverPhoto());
                        preparedStatement.setString(11, challenge.getShareUrl());
                        preparedStatement.setString(12, challenge.getQrCode());
                        preparedStatement.setString(13, challenge.getTopVideos());
                        preparedStatement.setString(14, challenge.getWeekGrowth());
                        preparedStatement.setLong(15, challenge.getUpdateTime());
                        preparedStatement.setString(16, challenge.getUpdateBy());
                        preparedStatement.setLong(17, challenge.getCreateTime());
                        preparedStatement.setString(18, challenge.getCreateBy());

                    }
                    @Override
                    public int getBatchSize() {
                        return challenges.size();
                    }
                });
    }
}
